﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BankLibrary;
using System.IO;

namespace _17._14_CreateFileSerializable
{
    public partial class CreateFileForm : BankUIForm
    {
        public CreateFileForm()
        {
            InitializeComponent();
        }

        private FileStream output;
        private BinaryFormatter formatter = new BinaryFormatter();

        private void saveButton_Click(object sender, EventArgs e)
        {
            DialogResult result;
            string fileName;

            using (SaveFileDialog fileChooser = new SaveFileDialog())
            {
                fileChooser.CheckFileExists = false;
                result = fileChooser.ShowDialog();
                fileName = fileChooser.FileName;
            }

            if (result == DialogResult.OK)
            {
                if (fileName != string.Empty)
                {
                    try
                    {
                        output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                        saveButton.Enabled = false;
                        enterButton.Enabled = true;
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void enterButton_Click(object sender, EventArgs e)
        {
            string[] values = GetTextBoxValues();

            Record record = new Record();

            if (values[(int)TextBoxIndices.ACCOUNT] != string.Empty)
            {
                try
                {
                    int accountNumber = Int32.Parse(values[(int) TextBoxIndices.ACCOUNT]);

                    if (accountNumber > 0)
                    {
                        record.Account = accountNumber;
                        record.FirstName = values[(int) TextBoxIndices.FIRST];
                        record.LastName = values[(int) TextBoxIndices.LAST];
                        record.Balance = Decimal.Parse(values[(int) TextBoxIndices.BALANCE]);

                        formatter.Serialize(output, record);
                    }
                    else
                    {
                        MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (SerializationException)
                {
                    MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void doneButton_Click(object sender, EventArgs e)
        {
            if (output != null)
            {
                try
                {
                    output.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Cannot close file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            Application.Exit();
        }
    }
}
